# Contributor: Guy Godfroy <guy.godfroy@gugod.fr>
# Maintainer: Guy Godfroy <guy.godfroy@gugod.fr>
pkgname=tofutf
pkgver=0.10.0
pkgrel=0
pkgdesc="Open source alternative to Terraform Enterprise"
url="https://docs.tofutf.io"
arch="x86_64 armhf armv7 aarch64" # terraform and opentofu pre-built binaries available only for those archs
license="MPL-2.0"
depends="tofutf-server tofutf-cli"
makedepends="go"
subpackages="
	$pkgname-cli
	$pkgname-server
	$pkgname-server-openrc:server_openrc
	$pkgname-agent
	$pkgname-agent-openrc:agent_openrc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/tofutf/tofutf/archive/refs/tags/v$pkgver.tar.gz
	tofutfd.initd
	tofutfd.confd
	tofutf-agent.initd
	tofutf-agent.confd
	"
options="net"

build() {
	mkdir build
	export LD_FLAGS="-s -w -X github.com/tofutf/tofutf/internal.Version=$pkgver"
	go build -ldflags="$LD_FLAGS" -o build ./...
}

check() {
	# TestExecutor_execute: takes ages and then timeout
	# internal/integration, Test{Pool,DB}: needs docker
	toskip="TestExecutor_execute|TestPool|TestDB"
	go test -skip "$toskip" $(go list ./... | grep -v internal/integration)
}

package() {
	# Main package is only a metapackage depending of server and cli component.
	# This is because main component should be server, but the binary named "tofutf" is the cli tool.
	# So to avoid confusion, all components are subpackaged.
	mkdir -p "$pkgdir"
}

cli() {
	unset depends
	pkgdesc="Command line tool for tofutf"
	install -m755 -D "$builddir"/build/tofutf \
		"$subpkgdir"/usr/bin/tofutf
}

server() {
	depends="xxd"
	pkgdesc="Server component for tofufs"
	install="tofutf-server.pre-install tofutf-server.post-install"
	install -m755 -D "$builddir"/build/tofutfd \
		"$subpkgdir"/usr/bin/tofutfd
}

server_openrc() {
	unset depends
	pkgdesc="Server component for tofufs (OpenRC init scripts)"
	install_if="$pkgname-server=$pkgver-r$pkgrel openrc"
	install -m755 -D "$srcdir"/tofutfd.initd \
		"$subpkgdir"/etc/init.d/tofutfd
	install -m644 -D "$srcdir"/tofutfd.confd \
		"$subpkgdir"/etc/conf.d/tofutfd
}

agent() {
	unset depends
	pkgdesc="Agent for tofutf"
	install="tofutf-agent.pre-install"
	install -m755 -D "$builddir"/build/tofutf-agent \
		"$subpkgdir"/usr/bin/tofutf-agent
}

agent_openrc() {
	unset depends
	install_if="$pkgname-agent=$pkgver-r$pkgrel openrc"
	pkgdesc="Agent for tofutf (OpenRC init scripts)"
	install -m755 -D "$srcdir"/tofutf-agent.initd \
		"$subpkgdir"/etc/init.d/tofutf-agent
	install -m644 -D "$srcdir"/tofutf-agent.confd \
		"$subpkgdir"/etc/conf.d/tofutf-agent
}

sha512sums="
1370f08a0593a629769b936765c5ff2aa617c5e4298c7a2743ffb42515ad7d43887496dc7d4194c607dc37b97062ba6f1542230b0a2b57c22392f6b28ccc606a  tofutf-0.10.0.tar.gz
267f21cb6aa0879132b994ff0a965b97cd426bf624b44b7e960cbfa2438405f3cd46f29cc6d76781ffe85705e828cc408dd67655125b696588041f822f30d71d  tofutfd.initd
f917e6078a21adec9a16923490c9b90e4ab03da59d488c11e29319ded8dccb5904be50c0c04563aa8268bc82c9d4bf50d579436f936b07f9010a17e8e9a4de99  tofutfd.confd
ae25de3409bc5e4ef8abb18eb2b2c56fc15ed57b272a2585e43e415e4a731c207c1c8349854a314ea0f5c2a9957d114ae6051d58de010cceb0b75ba67832912d  tofutf-agent.initd
7cb1869a722be8de24089d4fd368620efe4dc1768a141f842303077437f6cb96eec8d466a4feaa6a5bb220e6158fb55e7bc7ae260857dd42d6a1bbdd81e93f86  tofutf-agent.confd
"
